﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

using UncleBobWebService.Database;
using UncleBobWebService.Models;
using UncleBobWebService.ServiceRequester;


namespace UncleBobWebService
{
    public class UncleBobApplicantService : IWebService
    {
        //----------------------------------------------
        //----------------------------------------------


        //private static IDBAdapter dbAdapter = new ApplicantDBAdapter();
        private static string mConnectionString = ConfigurationManager.ConnectionStrings["AESDBConnectionString"].ConnectionString;
        //private static SqlConnection mConnection = new SqlConnection(mConnectionString);        
        //private static SqlCommand mCommand;

        // get rid of this
        public static Message mMsg = new Message();
            

        //***********************************************
        //***********************************************
        public Message RequestService(Message msg)
        {
            string action = msg.Category;
            Message thisMsg = new Message();
            try
            {
                switch (action)
                {                  
                    case MessageTypes.SERVICE_REGISTER_NEW_USER:
                        {
                            IDBAdapter dbAdapter = new ApplicantDBAdapter();
                            thisMsg = dbAdapter.CreateData(msg, msg.Data);
                            break;
                        }

                    case MessageTypes.SERVICE_GET_ALL_JOBS:
                        {
                            IDBAdapter dbAdapter = new StaffDBAdapter();
                            thisMsg = dbAdapter.SearchForAllData(msg);
                            break;
                        }
               
                    case MessageTypes.SERVICE_DELETE_REGISTERED_USER:
                        {
                            IDBAdapter db = new RegisterDBAdapter();
                            thisMsg = db.DeleteSpecificData(msg, msg.Data);
                            break;
                        }
                    case MessageTypes.SERVICE_GET_JOB_QUESTIONS:
                        {
                            IDBAdapter db = new QuestionDBAdapter();                           
                            thisMsg = db.SearchForAllData(msg);
                            break;
                        }
                    case MessageTypes.SERVICE_CREATE_QUESTIONS:
                        {
                            IDBAdapter db = new QuestionDBAdapter();
                            thisMsg = db.CreateData(msg, msg.Data);
                            break;
                        }
                    case MessageTypes.SERVICE_DELETE_JOB_QUESTION:
                        {
                            IDBAdapter db = new QuestionDBAdapter();
                            thisMsg = db.DeleteSpecificData(msg, msg.Data);
                            break;
                        }

                    case MessageTypes.SERVICE_GET_JOB_QUESTION:
                        {
                            IDBAdapter db = new QuestionDBAdapter();
                            thisMsg = db.SearchForSpecificData(msg, msg.Data);
                            break;
                        }

                    case MessageTypes.SERVICE_DELETE_ALL_JOB_QUESTIONS:
                        {
                            IDBAdapter db = new QuestionDBAdapter();
                            thisMsg = db.DeleteAllData(msg);
                            break;
                        }

                    case MessageTypes.SERVICE_GET_REGISTERED_USER:
                        {                            

                            IDBAdapter dbAdapter = new RegisterDBAdapter();
                            thisMsg = dbAdapter.SearchForSpecificData(msg, msg.Data );

                            RegisterModel tempModel = ((RegisterModel)thisMsg.Data).Clone();
                            Message tempMessage = new Message();

                            dbAdapter = new PermissionsDBAdapter();
                            tempMessage = dbAdapter.SearchForAllData(msg);
                            
                            // Copy the correct permissions over now.
                            tempModel.Permissions = ((RegisterModel)tempMessage.Data).Permissions;
                            thisMsg.Data = tempModel;                            
                            break;
                        }

                    case MessageTypes.SERVICE_ADD_NEW_JOB:
                        {
                            IDBAdapter dbAdapter = new StaffDBAdapter();
                            thisMsg = dbAdapter.CreateData(msg, msg.Data);
                            break;
                        }

                    case MessageTypes.SERVICE_GET_JOB_BY_TITLE:
                        {
                            IDBAdapter dbAdapter = new StaffDBAdapter();
                            thisMsg = dbAdapter.SearchForSpecificData(msg, msg.Data);
                            break;
                        }

                    case MessageTypes.SERVICE_DELETE_JOB_BY_TITLE:
                        {
                            StaffDBAdapter dbAdapter = new StaffDBAdapter();
                            thisMsg = dbAdapter.DeleteSpecificData(msg, msg.Data);
                            break;
                        }
                    case MessageTypes.SERVICE_VALIDATE_USER:
                        {
                            IDBAdapter dbAdapter = new RegisterDBAdapter();
                            thisMsg = dbAdapter.SearchForSpecificData(msg, msg.Data);
                            break;
                        }

                    case MessageTypes.SERVICE_GET_HR_SPECIAL_JOB_APPLICATION:
                        {
                            // Get the application.
                            JobApplication jobApp = msg.Data as JobApplication;

                            IDBAdapter dbAdapter = new ApplicationDBAdapter();
                            thisMsg = dbAdapter.SearchForAllData(msg);

                            // Get the job information
                            if (thisMsg.Result)
                            {
                                dbAdapter = new StaffDBAdapter();
                                thisMsg = dbAdapter.SearchForSpecificData(msg, msg.Data);

                                // Get the user questions.
                                if (thisMsg.Result)
                                {
                                    jobApp.Job = thisMsg.Data as JobModel;

                                    msg.Data = jobApp;
                                    dbAdapter = new UserAnswersDBAdapater();
                                    thisMsg = dbAdapter.SearchForSpecificData(msg, msg.Data);
                                }
                            }
                            break;
                        }
                    case MessageTypes.SERVICE_GET_USER_JOB_APPLICATION:
                        {
                            // THIS returns a JobApplication Object with ALL job information contained inside.
                            // Get the job application.
                            JobApplication jobApp = msg.Data as JobApplication;

                            IDBAdapter dbAdapter = new JobApplicationDBAdapter();  // DO NOT CHANGE THIS.
                            thisMsg = dbAdapter.SearchForSpecificData(msg, msg.Data);

                            // Get the job information
                            if (thisMsg.Result)
                            {                                
                                dbAdapter = new StaffDBAdapter();
                                thisMsg = dbAdapter.SearchForSpecificData(msg, msg.Data);

                                // Get the user questions.
                                if (thisMsg.Result)
                                {
                                    jobApp.Job = thisMsg.Data as JobModel;

                                    msg.Data = jobApp;
                                    dbAdapter = new UserAnswersDBAdapater();
                                    thisMsg = dbAdapter.SearchForSpecificData(msg, msg.Data);                               
                                }                                                                
                            }

                            break;
                        }

                    case MessageTypes.SERVICE_GET_USER_ALL_JOB_APPLICATIONS:
                        {
                            IDBAdapter dbAdapter = new JobApplicationDBAdapter();
                            thisMsg = dbAdapter.SearchForAllData(msg);
                            List<JobApplication> jobList = thisMsg.Data as List<JobApplication>;

                            if (thisMsg.Result & jobList.Count > 0)
                            {
                                foreach (JobApplication jobApp in jobList)
                                {
                                    thisMsg.Data = jobApp;
                                    // Get the job information
                                    if (thisMsg.Result)
                                    {
                                        dbAdapter = new StaffDBAdapter();
                                        thisMsg = dbAdapter.SearchForSpecificData(msg, msg.Data);

                                        // Get the user questions.
                                        if (thisMsg.Result)
                                        {
                                            jobApp.Job = thisMsg.Data as JobModel;

                                            msg.Data = jobApp;
                                            dbAdapter = new UserAnswersDBAdapater();
                                            thisMsg = dbAdapter.SearchForSpecificData(msg, msg.Data);
                                        }
                                    }
                                }
                            }

                            if (msg.Result)
                            {
                                KioskUser user = new KioskUser();
                                foreach (JobApplication jobApp in jobList)
                                {
                                    user.JobApplications.Add(jobApp.Job.Title, jobApp);
                                }

                                thisMsg.Data = user;
                            }

                            break;
                        }
                   
                    case MessageTypes.SERVICE_SET_USER_AS_STAFFING_EXPERT:
                        {
                            IDBAdapter dbAdapter = new PermissionsDBAdapter();
                            thisMsg = dbAdapter.CreateData(msg, msg.Data);
                            break;
                        }

                    case MessageTypes.SERVICE_REMOVE_USER_PERMISSIONS:
                        {
                            IDBAdapter dbAdapter = new PermissionsDBAdapter();
                            thisMsg = dbAdapter.DeleteSpecificData(msg, msg.Data);
                            break;
                        }

                    case MessageTypes.SERVICE_SAVE_JOB_APPLICATION:
                        {                           
                            // Save the users reponse                            
                            IDBAdapter dbAdapter = new JobApplicationDBAdapter();
                            thisMsg = dbAdapter.CreateData(msg, msg.Data);

                            if (thisMsg.Result)
                            {                                                              
                                dbAdapter = new UserAnswersDBAdapater();
                                thisMsg = dbAdapter.CreateData(msg, msg.Data);
                            }
                            break;
                        }


                    case MessageTypes.SERVICE_GET_JOB_APPLICANTS:
                        {
                            HiringManagerDBAdapter dbAdapter = new HiringManagerDBAdapter();
                            thisMsg = dbAdapter.SearchForSpecificData(msg, msg.Data);
                            
                            break;
                        }


                    case MessageTypes.SERVICE_EDIT_EXISTING_JOB:
                        {
                            // Save the users reponse
                            IDBAdapter dbAdapter = new StaffDBAdapter();
                            thisMsg = dbAdapter.EditSpecificData(msg, msg.Data);
                            break;
                        }


                    case MessageTypes.SERVICE_GET_MANAGER_JOB_APPLICATIONS:
                        {
                            // I didn't want to change a ton of the code outside of this area, so I made
                            // it return what you were expecting.

                            IDBAdapter dbAdapter = new HiringManagerDBAdapter();
                            thisMsg = dbAdapter.SearchForSpecificData(msg,msg.Data);
                                                        
                            HMRegisterModel model = thisMsg.Data as HMRegisterModel;
                            model.jobApps = new List<JobApplication>();                                                        

                            foreach (HMRegisterModel m in model.apps)
                            {
                                RegisterModel regMod = new RegisterModel();
                                regMod.UserID = int.Parse(m.UserID);
                                msg.Data = regMod;
                                dbAdapter = new JobApplicationDBAdapter();                                
                                thisMsg = dbAdapter.SearchForAllData(msg);
                                // Remove any that have been rejected.
                                List<JobApplication> filteredList = new List<JobApplication>();                                                                
                                foreach (JobApplication job in (thisMsg.Data as List<JobApplication>))
                                {
                                    if (!job.Rejected & !job.Hired)
                                    {
                                        msg.Data = job.Job;
                                        dbAdapter = new StaffDBAdapter();
                                        thisMsg = dbAdapter.SearchForSpecificData(msg, msg.Data);
                                        job.Job = thisMsg.Data as JobModel;                                        
                                        // Creates a list of all applications.
                                        model.jobApps.Add(job);
                                    }
                                }

                                m.jobApps = filteredList;
                            }

                            

                            thisMsg.Data = model;
                            break;
                        }                  

                    case MessageTypes.SERVICE_GET_USER_INFORMATION:
                        {
                            IDBAdapter dbAdapter = new ApplicantDBAdapter();
                            thisMsg = dbAdapter.SearchForAllData(msg);
                            break;
                        }

                    case MessageTypes.SERVICE_EDIT_JOB_QUESTION:
                        {
                            IDBAdapter dbAdapter = new QuestionDBAdapter();
                            thisMsg = dbAdapter.EditSpecificData(msg, msg.Data);
                            break;
                        }

                    case MessageTypes.SERVICE_EDIT_APP_STATUS:
                        {
                            IDBAdapter dbAdapter = new HiringManagerDBAdapter();
                            thisMsg = dbAdapter.CreateData(msg, msg.Data);
                            break;
                        }
                    case MessageTypes.SERVICE_EDIT_JOB_APPLICATION_STATUS:
                        {
                            IDBAdapter dbAdapter = new JobApplicationDBAdapter();
                            thisMsg = dbAdapter.EditSpecificData(msg, msg.Data);
                            break;
                        }

                    case MessageTypes.SERVICE_ADD_COMMENT:
                        {
                            IDBAdapter dbAdapter = new HMCommentDBAdapter();
                            thisMsg = dbAdapter.CreateData(msg, msg.Data);
                            break;
                        }

                    case MessageTypes.SERVICE_GET_COMMENT:
                        {
                            IDBAdapter dbAdapter = new HMCommentDBAdapter();
                            thisMsg = dbAdapter.SearchForSpecificData(msg, msg.Data);
                            break;
                        }

                    case MessageTypes.SERVICE_EDIT_COMMENT:
                        {
                            IDBAdapter dbAdapter = new HMCommentDBAdapter();
                            thisMsg = dbAdapter.EditSpecificData(msg, msg.Data);
                            break;
                        }

                    case MessageTypes.SERVICE_EDIT_REGISTRATION:
                        {
                            IDBAdapter dbAdapter = new RegisterDBAdapter();
                            thisMsg = dbAdapter.EditSpecificData(msg, msg.Data);
                            break;
                        }
                }
            }
            catch (Exception ex)
            {
                thisMsg.Data = string.Format("Exception {0} thrown", ex.Message);
            }

            msg = thisMsg;
            return msg;
        }


        public Message GetData()
        {
            return mMsg;

        }       
    }
}
